package sctu.edu.java.year2016.team08.lisonghong.day20160616.chapter12_1;

import javax.swing.*;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by 松红 on 2016/6/16.
 * 实现读取文件时出现一个表示进度的进度条，可使用ProgressMonitorInputStream。
 */
public class Test extends JFrame {
    public static void main(String[] args) throws IOException, InterruptedException {

        JFrame jFrame = new JFrame("读取文件进度条");
        jFrame.setSize(200, 200);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        ProgressMonitorInputStream monitor;
        monitor = new ProgressMonitorInputStream(jFrame, "读取数据", new FileInputStream("D:/word.txt"));
        ProgressMonitor progressMonitor = monitor.getProgressMonitor();

        int read_unit = 2;
        int all = monitor.available();
        int readed = 0;

        byte[] data = new byte[read_unit];

        while (monitor.available() > 0) {
            Thread.sleep(1000);
            int in = monitor.read(data);
            readed += in;
            float process = (float) readed / all * 100;
            progressMonitor.setNote("archived:" + process + "%");
            System.out.println("archived " + process + " %");
        }
    }
}
